export const ChannelFormMixins = {
    data() {
        return {
            insert_data_rules: {
                "infoResponseModel.highSeas": [
                    {required: true, message: '请选择是否查看公海', trigger: 'blur'}
                ],
                "infoResponseModel.distribution": [
                    {required: true, message: '请选择是否分配客户', trigger: 'blur'}
                ],
                "infoResponseModel.cap": [
                    {required: true, message: '请输入私海名额', trigger: 'blur'}
                    ,{
                        validator: (rule, value, call) => {
                            this.validateReg(value,  /^[1-9][0-9]{0,8}$/, "请输入1-9位数字", call);
                        },
                        trigger: 'blur'
                    }
                ],
                "timeNodeResponseModel.protectTime": [
                    {required: true, message: '请输入私海保护期', trigger: 'blur'}
                    ,{
                        validator: (rule, value, call) => {
                            this.validateReg(value,  /^[1-9][0-9]{0,8}$/, "请输入1-9位数字", call);
                        },
                        trigger: 'blur'
                    }
                ],
                "timeNodeResponseModel.expandTime": [
                    {required: true, message: '请输入延迟私海保护期', trigger: 'blur'}
                    ,{
                        validator: (rule, value, call) => {
                            this.validateReg(value,  /^[1-9][0-9]{0,8}$/, "请输入1-9位数字", call);
                        },
                        trigger: 'blur'
                    }
                ],
                "timeNodeResponseModel.visitTime": [
                    {required: true, message: '请输入拜访刷新时间', trigger: 'blur'}
                    ,{
                        validator: (rule, value, call) => {
                            this.validateReg(value,  /^[1-9][0-9]{0,8}$/, "请输入1-9位数字", call);
                        },
                        trigger: 'blur'
                    }
                ],
                "timeNodeResponseModel.orderTime": [
                    {required: true, message: '请输入发布订单刷新时间', trigger: 'blur'}
                    ,{
                        validator: (rule, value, call) => {
                            this.validateReg(value,  /^[1-9][0-9]{0,8}$/, "请输入1-9位数字", call);
                        },
                        trigger: 'blur'
                    }
                ],
                "timeNodeResponseModel.publishTime": [
                    {required: true, message: '请输入发布采购需求刷新时间', trigger: 'blur'}
                    ,{
                        validator: (rule, value, call) => {
                            this.validateReg(value,  /^[1-9][0-9]{0,8}$/, "请输入1-9位数字", call);
                        },
                        trigger: 'blur'
                    }
                ],
                //公司验证
                'certificationResponseModel.companyCnName': [
                    {required: true, message: '请输入公司名称', trigger: 'blur'}
                ],
                "certificationResponseModel.legalPerson": [
                    {required: true, message: '请输入公司法人', trigger: 'blur'}
                ],
                "certificationResponseModel.idCardNumber": [
                    {required: true, message: '请输入身份证号码', trigger: 'blur'}
                    ,{
                        validator: (rule, value, call) => {
                            this.isIdcard(value, call);
                        },
                        trigger: 'blur'
                    }

                ],
                "infoResponseModel.maiboxl": [
                    {required: true, message: '请输入账号/Email', trigger: 'blur'}
                    ,{
                        validator: (rule, value, call) => {
                            this.isEmail(value, call);
                        },
                        trigger: 'blur'
                    }
                ],
                "infoResponseModel.userName": [
                    {required: true, message: '请输入用户名', trigger: 'blur'}
                ],
                "infoResponseModel.phone": [
                    {required: true, message: '请输入手机号码', trigger: 'blur'}
                    ,{
                        validator: (rule, value, call) => {
                            this.isPhone(value, call);
                        },
                        trigger: 'blur'
                    }
                ],

                'companyCnName': [
                    {required: true, message: '请输入公司名称', trigger: 'blur'}
                ],
                "mail": [
                    {required: true, message: '请输入Email', trigger: 'blur'}
                    ,{
                        validator: (rule, value, call) => {
                            this.isEmail(value, call);
                        },
                        trigger: 'blur'
                    }
                ],
                "userName": [
                    {required: true, message: '请输入用户名', trigger: 'blur'}
                ],
                "phone": [
                    {required: true, message: '请输入手机号码', trigger: 'blur'}
                    ,{
                        validator: (rule, value, call) => {
                            this.isPhone(value, call);
                        },
                        trigger: 'blur'
                    }
                ]
            }
        }
    },
    methods: {
        /**
         * 修改账号权限信息
         */
        modifyAuthority() {
            this.$refs[this.formName].validate((valid) => {
                if (valid) {
                    let areaArr = this.checkCity();
                    let ret;
                    if (this.type == 1) {
                        ret = this.baseController.setAuthority({
                            infoRequestModel: this.insert_data.infoResponseModel,
                            timeNodeRequestModel: this.insert_data.timeNodeResponseModel,
                            areaModel: areaArr,
                            list: this.instrduy()
                        })
                    } else {
                        ret = this.baseController.modifyAuthority({
                            infoRequestModel: this.insert_data.infoResponseModel,
                            timeNodeRequestModel: this.insert_data.timeNodeResponseModel,
                            areaModel: areaArr,
                            list: this.instrduy()
                        })
                    }
                    ret.then(res => {
                        this.successMessage("修改成功");
                        this.back();
                        return res;
                    });
                } else {
                    return false;
                }
            });

        },
        instrduy() {
            let list = [];
            try {
                let classifly = this.$refs.classifly.$refs.classifly.getCheckedNodes(false, true);
                if (classifly == null || classifly.length < 1) {
                    return list;
                }
                classifly.forEach(res => {
                    let da = {
                        type: 2,
                        channelId: this.insert_data.infoResponseModel.id,
                        userId: this.insert_data.infoResponseModel.userId
                    };
                    switch (res.data.level) {
                        case 1:
                            da["primaryName"] = res.data.name;
                            da["primaryNameEn"] = res.data.nameEn;
                            da["primaryId"] = res.data.id;
                            break;
                        case 2:
                            //国别
                            da["secondaryName"] = res.data.name;
                            da["secondaryNameEn"] = res.data.nameEn;
                            da["secondaryId"] = res.data.id;
                            da["primaryName"] = res.data.levelFirstName;
                            da["primaryNameEn"] = res.data.levelFirstNameEn;
                            da["primaryId"] = res.data.levelFirstId;
                            break;
                    }
                    list.push(da);
                })
            } catch (e) {
                return list;
            }
            return list;
        },
        checkCity() {
            let areaModel = [];
            try {
                let nodes = this.$refs.area.$refs.area.getCheckedNodes().filter(item => ((item.parent == null && item.checked) || !(item.parent != null && item.parent.checked == true)));
                if (nodes != null && nodes.length > 0) {
                    for (let n of nodes) {
                        areaModel.push({
                            id: n.data.id,
                            level: n.data.level,
                            channelId: this.insert_data.infoResponseModel.id,
                            userId: this.insert_data.infoResponseModel.userId
                        });
                    }
                }
            } catch (e) {

            }
            return areaModel;
        }

    }
}
